/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'package:flutter/material.dart';
import 'package:hadss_intents/hadss_intents.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String printString = "";
  final _intentsPlugin = Intents();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          textDirection: TextDirection.ltr,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          mainAxisAlignment: MainAxisAlignment.start,
          mainAxisSize: MainAxisSize.max,
          verticalDirection: VerticalDirection.down,
          children: [
            Text(printString),
            TextButton(
                onPressed: () {
                  doGetSide();
                },
                child: const Text("getSide")),
            TextButton(
                onPressed: () {
                  doDeleteIntent();
                },
                child: const Text("deleteIntent")),
            TextButton(
                onPressed: () {
                  doDeleteEntity();
                },
                child: const Text("deleteEntity")),
            TextButton(
                onPressed: () {
                  try {
                    doPlayMusic();
                  } catch (e) {
                    setState(() {
                      printString = 'doPlayMusic error = ${e.toString()}';
                    });
                  }
                },
                child: const Text("ShareIntent类型: PlayMusic")),
            TextButton(
                onPressed: () {
                  try {
                    doPlayVideo();
                  } catch (e) {
                    setState(() {
                      printString = 'doPlayVideo error = ${e.toString()}';
                    });
                  }
                },
                child: const Text("ShareIntent类型: PlayVideo"))
          ],
        ),
      ),
    );
  }

  // 分享意图信息
  void doPlayMusic() {
    List<InsightIntent> list = [];
    InsightIntent intent = InsightIntent();
    intent.identifier = "52dac3b0-6520-4974-81e5-25f0879449b5";
    intent.intentVersion = "1.0";
    intent.intentName = "PlayMusic";
    intent.intentActionInfo = {
      "actionMode": "EXECUTED",
      "executedTimeSlots": {
        "executedStartTime": 1747328400000,
        "executedEndTime": 1747328452000
      },
      "currentPercentage": 50
    };
    intent.intentEntityInfo = {
      "entityName": "Music",
      "entityId": "C10194368",
      "entityGroupId": "C10194321312",
      "displayName": "flutter测试歌曲",
      "description": "NA",
      "logoURL":
          "https://www-file.huawei.com/-/media/corporate/images/home/logo/huawei_logo.png",
      "keywords": ["华为音乐", "化妆"],
      "rankingHint": 99,
      "expirationTime": 1747328452000,
      "metadataModificationTime": 1747328452000,
      "activityType": ["1", "2", "3"],
      "artist": ["测试歌手1", "测试歌手2"],
      "lyricist": ["测试词作者1", "测试词作者2"],
      "composer": ["测试曲作者1", "测试曲作者2"],
      "albumName": "测试专辑",
      "duration": 244000,
      "playCount": 100000,
      "musicalGenre": ["流行", "华语", "金曲", "00后"],
      "isPublicData": false
    };
    list.add(intent);
    _intentsPlugin.shareIntent(list).then(
        (value) => {
              setState(() {
                printString = 'doPlayMusic : success';
              })
            },
        onError: (err) => {
              setState(() {
                printString = 'doPlayMusic : error $err';
              })
            });
  }

  void doGetSide() {
    try {
      _intentsPlugin.getSide(true).then(
          (value) => {
                setState(() {
                  printString = 'doGetSide : $value';
                })
              },
          onError: (err) => {
                setState(() {
                  printString = 'doGetSide : error $err';
                })
              });
    } catch (e) {
      setState(() {
        printString = 'doGetSide error = ${e.toString()}';
      });
    }
  }

  void doDeleteIntent() {
    try {
      _intentsPlugin.deleteIntent('PlayMusic').then(
          (value) => setState(() {
                printString = 'doDeleteIntent : success';
              }),
          onError: (err) => {
                setState(() {
                  printString = 'doDeleteIntent : error $err';
                })
              });
    } catch (e) {
      setState(() {
        printString = 'doDeleteIntent error = ${e.toString()}';
      });
    }
  }

  void doDeleteEntity() {
    try {
      _intentsPlugin.deleteEntity('Music', ['C10194368']).then(
          (value) => setState(() {
                printString = 'doDeleteEntity : success';
              }),
          onError: (err) => {
                setState(() {
                  printString = 'doDeleteEntity : error $err';
                })
              });
    } catch (e) {
      printString = 'doDeleteEntity error = ${e.toString()}';
    }
  }

  void doPlayVideo() {
    List<InsightIntent> list = [];
    InsightIntent intent = InsightIntent();
    intent.identifier = "86d23b0-6520-5674-81e5-2136b5";
    intent.intentVersion = "1.0";
    intent.intentName = "PlayVideo";
    intent.intentActionInfo = {
      "actionMode": "EXPECTED",
      "currentSerialNumber": 1,
      "currentPercentage": 50
    };
    intent.intentEntityInfo = {
      "entityName": "Video",
      "entityId": "12949589",
      "entityGroupId": "129495",
      "displayName": "flutter测试video",
      "description":
          "该剧讲述了张三和李四为了摆脱家庭贫苦而努力奋斗的励志故事。",
      "logoURL":
          "https://raw.gitcode.com/openharmony-sig/flutter_ohfeatures/files/master/packages/intents/screenshorts/deleteEntity.png",
      "keywords": ["视频"],
      "artists": ["张三", "李四"],
      "category": "电视剧",
      "tags": ["都市", "家庭"],
      "playType": "正片",
      "score": 7.4,
      "rankingHint": 99,
      "videoName": "测试video",
      "videoImage":
          "https://raw.gitcode.com/openharmony-sig/flutter_ohfeatures/files/master/packages/intents/screenshorts/deleteEntity.png"
    };

    list.add(intent);
    _intentsPlugin.shareIntent(list).then(
        (value) => {
              setState(() {
                printString = 'doPlayVideo : success';
              })
            },
        onError: (err) => {
              setState(() {
                printString = 'doPlayVideo : error $err';
              })
            });
  }
}
